<template>
  <div class="login_container">
    <div class="login_box">
      <!--头像区域-->
      <div class="logo_box">
        <img
          src="../assets/bjr.png"
          alt="logo"
        >
      </div>
      <!--登录表单区域-->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_from"
      >
        <!--用户名-->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
            placeholder="邮箱/手机号码"
            clearable
          />
        </el-form-item>
        <!--密码-->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            placeholder="密码"
            show-password
            clearable
          />
        </el-form-item>
        <!--验证码-->
        <!--
          <el-form-item>
           <el-input v-model="form.captcha_code"
                      placeholder="验证码"
                      autocomplete="off"
                      autocapitalize="off"
                      spellcheck="false"
                      maxlength="4"
                      style="float: left; width: 122px;"></el-input>
            <div class="captcha_code">
              <img src="" ref="code" @click="changeCode">
            </div>-->
        <!--  <el-input class="input" maxlength="8" placeholder="请输入验证码"></el-input>
          <div class="divIdentifyingCode" @click="getIdentifyingCode(true)">
            <img id="imgIdentifyingCode" style="height:40px; width: 100px; cursor: pointer;" alt="点击更换"
                 title="点击更换" />
          </div>
        </el-form-item>-->

        <!--按钮区域-->
        <el-form-item class="btns">
          <el-button
            type="primary"
            @click="login">
            登录
          </el-button>
          <el-button
            type="info"
            @click="resetLoginForm">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'Login',
    data() {
      return {
        flag: false,
        // 这是登录表单的数据绑定对象
        loginForm: {
          username: '822807097@qq.com',
          password: '123456'
          /* captcha_key: '',
            captcha_code: '' */
        },
        loginFormRules: {
          // 验证用户名是否合法
          username: [
            { required: true, message: '请输入登录名称', trigger: 'blur' },
            // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
            {
              validator: (rule, value, callback) => {
                const email = /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/// 邮箱验证
                const MobileRegex = /0?(13|14|15|18)[0-9]{9}/// 手机号码验证
                if (!MobileRegex.test(value) && !email.test(value)) {
                  this.flag = false
                  callback(new Error('用户名格式不正确!！'))
                } else {
                  this.flag = true
                }
                callback()
              },
              trigger: 'blur'
            }
          ],
          // 验证密码是否合法
          password: [
            { required: true, message: '请输入登录密码', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      // this.changeCode()
      /*
      const enter = this
      document.onkeydown = function(e) {
        const key = window.event.keyCode
        if (key === 13) {
          enter.login()
        }
      }
      */
      document.addEventListener('keydown', (e) => {
        const key = window.event.keyCode
        if (key === 13) {
          this.login()
        }
      })
    },
    methods: {
      // 点击重置按钮，重置登录表单
      resetLoginForm() {
        this.$refs.loginFormRef.resetFields()
      },
      login() {
        this.$refs.loginFormRef.validate(async valid => {
          if (!valid && !this.flag) return
          const { data: res } = await this.$http.post('login', this.loginForm)
          if (res.meta.status !== 200) {
            return this.$message.error('登录失败,' + res.meta.msg)
          }
          this.$message.success('登录成功')
          /*
            1.
            将登录成功之后的token,保存到客户端的 sessionStorage 中
            项目中除了登录之外的其他API接口,必须在登录之后才能访问
            token 只应在当前网站打开期间生效,所以将 token 保存在 sessionStorage 中
            */
          window.sessionStorage.setItem('token', res.data.token)
          // 2.通过编程式导航跳转到后台主页，路由地址是/home
          this.$router.push('/home')// 登录成功跳转后台主页
        })
      }
      /* getCaptchaKey () {
          let key=Math.random().toString(36).substring(10);
          console.log(key);
          return key
       },
       changeCode () {
         const captcha_key = this.getCaptchaKey();
         this.form.captcha_key = captcha_key;
         this.$refs.code.setAttribute(
           'src', process.env.VUE_APP_API_URL + 'auth/get_captcha?captcha_key=' + captcha_key
         );
       },
       getIdentifyingCode(bRefresh) {
         let identifyCodeSrc =  "../assets/bjr.png";
         if (bRefresh) {
           identifyCodeSrc =  "../assets/bjr.png"+Math.random();
         }
         let objs = document.getElementById("imgIdentifyingCode");
         objs.src = identifyCodeSrc;
       }, */
    }
  }
</script>

<style lang="less" scoped>
  .login_container {
    //background-color: #2b4b6b;
    background-image: url("../assets/images/fengjing.jpg");
    height: 100%;
  }

  .login_box {
    width: 400px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }

  .logo_box {
    height: 100px;
    width: 100px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }

  .btns {
    display: flex;
    justify-content: flex-end;
  }

  .login_from {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }

  /*  .captcha_code {
      width: 100px;
      height: 100px;
      background-color: rebeccapurple;
    }*/
  /*  .captcha_code{
      height: 35px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }*/
  /*  .divIdentifyingCode {
      position: absolute;
      top: 0;
      right: 0;
      z-index: 5;
      width: 102px; !*设置图片显示的宽*!
      height: 40px; !*图片显示的高*!
      background: #e2e2e2;
      margin: 0;
    }*/
</style>
